package com.kefu.exam.controller;

import com.kefu.exam.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.exam.service.ExamPaperSubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 * @author xy
 * @date 2022/02/28
 */
@Api(tags = "问卷问题")
@RestController
@RequestMapping("exam/paperSubject")
public class ExamPaperSubjectController extends AbstractBaseController {

    @Autowired
    private ExamPaperSubjectService examPaperSubjectService;

    @PostMapping("/add")
    @ApiOperation(value = "新增", notes = "新增")
    public Result<String> add(@Validated @RequestBody ExamPaperSubjectInsertVo examPaperSubjectVo) {
    	logger.info("新增:{}", examPaperSubjectVo);
    	
        return ResultGenerator.genSuccessResult(examPaperSubjectService.addPaperSubject(examPaperSubjectVo));
    }

    @PostMapping("/batchAdd")
    @ApiOperation(value = "批量添加试题到试卷中", notes = "批量添加试题到试卷中")
    public Result<String> batchAdd(@Validated @RequestBody ExamPaperSubjectBatchInsertVo vo) {
        logger.info("批量添加试题到试卷中:{}", vo);

        return ResultGenerator.genSuccessResult(examPaperSubjectService.batchAdd(vo.getPaperId(), vo.getSubjectIds()));
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除", notes = "删除")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
    	logger.info("删除:{}", delete);
    	
        examPaperSubjectService.deleteById(delete.getId());
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/arraySort")
    @ApiOperation(value = "问卷问题顺序", notes = "问卷问题顺序")
    public Result<String[]> arraySort(@Validated @RequestBody String[] sort, Integer start, Integer end) {
        logger.info("问卷问题顺序:{}", sort);

        return ResultGenerator.genSuccessResult(examPaperSubjectService.moveSort(sort, start, end));
    }

    @PostMapping("/sort")
    @ApiOperation(value = "问卷问题排序", notes = "问卷问题排序")
    public Result<Integer> sort(@Validated @RequestBody ExamPaperSubjectSortVo voList) {
        logger.info("问卷问题排序:{}", voList);

        return ResultGenerator.genSuccessResult(examPaperSubjectService.movingSubject(voList));
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询问卷问题", notes = "查询问卷问题")
    public Result<List<ExamCascadeSubjectVo>> list(@Validated @RequestBody ExamPaperSubjectSearchVo vo) {
        logger.info("查询问卷问题:{}", vo);

        return ResultGenerator.genSuccessResult(examPaperSubjectService.searchAllSubject(vo));
    }
}
